﻿import { Employee } from './../domain/model/employee';
import { DataContextFactory } from './../domain/fbsDataContext';
import { ServiceException } from "./serviceException";
import { Guid, Logger, ObjectCopier, intersectionArray } from "../common/utils";
import { Transaction } from "tiny-entity";
import * as R from "ramda";

export class EmployeeService {
    private get ctx() {
        return DataContextFactory.GetDataContext();
    }

    get ServiceName() {
        return "EmployeeService";
    }

    async CleanData() {
    }

    async getEmployeeByRole(roleName:string) {
        let employeeList = await this.ctx.Employee.ToList();
        let result = employeeList.filter(function(e){
            if(e.roles){
                let isWaiter = e.roles.find(function(r){
                    return r.name == roleName;
                });
                if(isWaiter){
                    return true;
                }
            }
            return false;
        });
        return result;
    }

    public async findEmployees() {
        return await this.ctx.Employee.ToList();
    }

    findEmployeeById(id:string){
        return this.ctx.Employee.First(x=>x.id==id,["id"],[id])
    }

    async getEmployeeById(id:string){
        const employee = await this.findEmployeeById(id);
        if(!employee) throw new ServiceException(this, id+"没有找到");
        return employee;
    }

    async updateEmployee(employee:Employee):Promise<Employee>{
        const oldEmployee = await this.getEmployeeById(employee.id);
        return await this.ctx.Update(R.merge(oldEmployee, employee));
    }
}